﻿using Enum;
using MediatR;
using Practice.API.Application.Commands.Permissionw;
using Practice.Common.Domains;
using Practice.Common.Dto;
using Practice.Common.Infrastructure.Interfaces;

namespace Practice.API.Application.Commands.Roles
{
    public class RoleDelHandler : IRequestHandler<RoleDelCommand, Result>
    {
        public readonly IBaseRepository<Role> RoleRepository;

        public RoleDelHandler(IBaseRepository<Role> roleRepository)
        {
            RoleRepository = roleRepository;
        }

        public async Task<Result> Handle(RoleDelCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.Success };
            try
            {
                var res = RoleRepository.GetAll().Where(x => x.RoleId == request.RoleId).FirstOrDefault();
                if (res != null)
                {
                    res.RoleStatus = true;
                    RoleRepository.Update(res);
                    result.Code = (int)CommonEnumCode.Success;
                }
                return result;

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
